#!/usr/bin/env python3

import codecs
import json
import pymongo
import time
import http.client, urllib.request, urllib.parse, urllib.error, base64,urllib.response
from config import *
from multiprocessing import Pool
from bson import ObjectId
#配置mongodb的信息
client = pymongo.MongoClient(MONGO_URL)
db = client[MONGO_DB]
fail_response = []

'''
调用接口获取数据
'''
def get_data(university_name):
	headers = {
		# e321e00f876b41989a89b56887161602
		'Ocp-Apim-Subscription-Key': 'e321e00f876b41989a89b56887161602',
	}
	params = urllib.parse.urlencode({
	    # Request parameters
		# 'expr': "Or(And(Composite(C.CN=='chi'),Composite(AA.AfN=='" + university_name + "')) , And(Composite(C.CN=='sigcse'),Composite(AA.AfN=='" + university_name + "')))",
	    # 'expr': "Or(And(Composite(C.CN=='embc'),Composite(AA.AfN=='" + university_name + "')) , And(Composite(C.CN=='cvpr'),Composite(AA.AfN=='" + university_name + "')))",
		# 'expr': "And(composite(C.CN=='" + conference_name + "'),Composite(AA.AfN=='" + university_name + "'))",
		'expr': "And(composite(AA.AfN=='" + university_name + "'),Y>2008)",
		'model': 'latest',
	    'count': '1000',
	    'offset': '7100',
	    'orderby': 'Y',
	    'attributes': 'Ti,Y,CC,ECC,AA.AuN,AA.AuId,AA.AfN,AA.AfId,AA.S,F.FN,F.FId,J.JN.J.JID,C.CN,C.CId',
	})

	conn = http.client.HTTPSConnection('api.labs.cognitive.microsoft.com')
	conn.request("GET", "/academic/v1.0/evaluate?%s"%params,"",headers)
	response = conn.getresponse()

	if response.status == 200:
		# print(response.status)
		print("获取以下大学的数据")
		print(university_name)
		json_data = str(response.read(),encoding = "utf-8")
		conn.close()
		return json_data
	else:
		print("调用接口失败!")
		print("-----------------------------------------------------")
		print(university_name)
		fail_response.append(university_name)
		print(response.read())
		conn.close()
		return None

'''
将字符串形式转为json格式,并逐条保存数据
'''
def str_to_json(json_data,university_name):
	json_entities_data = json_data["entities"]
	for item in json_entities_data:
		# item["_id"] = ObjectId()
		# print(item["_id"])
		# print(item)
		# save_to_mongo(item,university_name)
		pass


'''
插入到mongodb中
'''
def save_to_mongo(result,university_name):
	if db[university_name].insert(result):
		# print("插入" + university_name + "成功")
		return True
	# print("插入" + university_name + "失败")
	return False


'''
主函数
'''
def demo(university_name):
	json_data = get_data(university_name)
	json_data = json.loads(json_data)
	if len(json_data["entities"]) == 0:
		print("以下学校查找paper为0")
		print(university_name)
	else:
		print(len(json_data["entities"]))
		# str_to_json(json_data,university_name)
	# time.sleep(5)


def main():
	list = [i*1000 for i in range(20)]
	# pool = Pool()
	# pool.map(demo,UNIVERSITY_LIST)
	for university_name in UNIVERSITY_LIST:
		demo(university_name)


if __name__ == '__main__':
	main()
